﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：表单权限字段导入
 */
var id = request('id');
var companyId = request('companyId');

var acceptClick;
var bootstrap = function ($, learun) {
    "use strict";

    var page = {
        init: function () {
            page.bind();
            page.initData();
        },
        bind: function () {


            $('#field').lrselect({
                placeholder: '请选择字段',
                maxHeight: 80,
                allowSearch: true
            });
            $('#formId').lrselect({
                placeholder: '请选择表单',
                text: 'name',
                value: 'id',
                url: config.base_server() + 'customerforms/list?companyId='+companyId+'&access_token=' + config.getToken().access_token,
                maxHeight: 140,
                allowSearch: true,
                select: function (item) {
                    if (!!item) {
                        var formName = item.name;
                        var url = config.webportal_server + '/pages/form/custmerform/workflowInstanceForm.html?id=' + item.id + '&access_token=' + config.getToken().access_token;
                        $('#name').val(formName);
                        $('#url').val(url);
                        $('#code').val(item.code);
                        $.lrSetForm(config.base_server() + 'customerforms/' + item.id, function (data) {
                            var scheme = JSON.parse(data.scheme.scheme);
                            var fields = [];
                            for (var i = 0, l = scheme.data.length; i < l; i++) {
                                var componts = scheme.data[i].componts;
                                for (var j = 0, jl = componts.length; j < jl; j++) {
                                    var compont = componts[j];
                                    if (!!compont.title && !!compont.field) {
                                        var point = {text: compont.title, id: compont.id};
                                        fields.push(point);
                                    }
                                }
                            }
                            $('#field').lrselectRefresh({data: fields});
                        });
                    }
                    else {
                        $('#field').lrselectRefresh({data: []});
                    }
                }
            });
            $('#lr_preview').on('click', function () {
                var formId = $('#formId').lrselectGet();
                if (!!formId) {
                    learun.layerForm({
                        id: 'custmerForm_PreviewForm',
                        title: '预览当前表单',
                        url: config.webportal_server + 'pages/form/custmerform/previewForm.html?schemeInfoId=' + formId + '&access_token=' + config.getToken().access_token,
                        width: 800,
                        height: 600,
                        maxmin: true,
                        btn: null
                    });
                }
                else {
                    learun.alert.warning('请选择表单！');
                }
            });


            $('#type').lrselect({
                data: [{id: '1', text: '自定义表单'}, {id: '0', text: '系统表单'}],
                placeholder: false,
                maxHeight: 80,
                select: function (item) {
                    if (item.id != '1') {
                        $('.custmer-form').hide();
                    }
                    else {
                        $('.custmer-form').show();
                    }
                }
            }).lrselectSet('1');

            $('#upload').lrselect({
                data: [{id: '2', text: '不允许'}, {id: '1', text: '允许'}],
                placeholder: false,
                maxHeight: 80,
            }).lrselectSet('2');
        },
        initData: function () {
            if (!!id) {
                var workforms = top.layer_NodeForm.workforms;
                for (var i = 0, l = workforms.length; i < l; i++) {
                    if (workforms[i].id == id) {
                        $('#form').lrSetFormData(workforms[i]);
                        break;
                    }
                }
            }
        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        var formdata = $('#form').lrGetFormData();
        formdata.id = id;
        if (formdata.type != '1') {
            formdata.formId = '';
        }
        callBack(formdata);
        return true;
    };
    page.init();
}